package com.whitehatsec.sentineldataexchange;

import com.whitehatsec.sentineldataexchange.model.WhiteHatSite;
import com.whitehatsec.sentineldataexchange.model.WhiteHatVulnerability;
import java.util.Properties;

/**
 * This is an example class that demonstrates how to implement the IDataExchanger interface. 
 * It loads sites and vulnerabilities from Sentinel, but does not process them. 
 * 
 * A real implementation would place processing logic at the 'DO SOMETHING' comment below. 
 * 
 * @author August Detlefsen [augustd@codemagi.com]
 */
public class DataExchanger implements IDataExchanger {

	private Properties properties;
	private transient SentinelLogin login;
	private Integer siteID;

	public void exchangeData() {

		try {
			// Load up the library of sites
			WhiteHatSiteLibrary whSiteLib = new WhiteHatSiteLibrary(properties);

			//load up the specific site to get vulns for
			WhiteHatSite site = whSiteLib.loadSiteDetails(login, siteID);

			// Load up the library of vulnerabilities
			WhiteHatVulnerabilityLibrary whVulnLib = new WhiteHatVulnerabilityLibrary(properties);
			whVulnLib.loadVulnerabilities(login, siteID);

			//convert the WhiteHat vulnerabilities into ???
			if (whVulnLib.getNumberOfVulnerabilities() > 0) {

				for (WhiteHatVulnerability vuln : whVulnLib.getVulnerabilities()) {
					//parse out the vuln data
					String vulnUrl = vuln.getRequestUrl();
					String host = Utils.getHost(vulnUrl);
					String vulnId = vuln.getVulnID();
					String vulnClass = vuln.getVulnClass();
					String dateFound = vuln.getDateFound();
					int severity = vuln.getSeverity();
					int threat = vuln.getThreat();
					int score = vuln.getScore();
					String description = Utils.trimRedundantWhitespace(Utils.stripHtmlRegex(vuln.getDescription()));
					String attackVectors = Utils.trimRedundantWhitespace(Utils.stripHtmlRegex(vuln.getAttackVectors()));
					String solution = Utils.trimRedundantWhitespace(Utils.stripHtmlRegex(vuln.getSolution()));

					//DO SOMETHING ---------------------------------------
				}
			}

		} catch (Exception e) {
			System.err.println("Operation discontinued.");
			e.printStackTrace(System.err);

		}

	}

	public void setProperties(Properties props) {
		this.properties = props;
	}

	public void setSiteID(Integer newValue) {
		this.siteID = newValue;
	}

	public void setLogin(SentinelLogin login) {
		this.login = login;
	}
}
